package cn.ac.jaas.ljx.wordsegmentation.util;

import java.io.File;
import java.util.List;

import cn.ac.jaas.ljx.wordsegmentation.domain.Word;

import jxl.Workbook;
import jxl.write.*;
import jxl.write.Number;

public class DocumentWriteUtil {

	public static void excelWriteResult(File file, List<Word> words ) {
		try {
			WritableWorkbook writableWorkbook = Workbook.createWorkbook(file);
			WritableSheet writableSheet_all = writableWorkbook.createSheet("result_all", 0);
			WritableSheet writableSheet_filter= writableWorkbook.createSheet("result_filter", 1);	
			Label titleLabel1 = new Label(0, 0, "词");
			writableSheet_all.addCell(titleLabel1);
			Label titleLable1_filter_sheet = new Label(0, 0, "词");
			writableSheet_filter.addCell(titleLable1_filter_sheet);
			Label titleLabel2 = new Label(1, 0, "词频");
			writableSheet_all.addCell(titleLabel2);
			Label titleLable2_filter_sheet = new Label(1, 0, "词频");
			writableSheet_filter.addCell(titleLable2_filter_sheet);
			Label titleLabel3 = new Label(2, 0, "词性");
			writableSheet_all.addCell(titleLabel3);
			Label titleLable3_filter_sheet = new Label(2, 0, "词性");
			writableSheet_filter.addCell(titleLable3_filter_sheet);
			for(int i=1,j=1; i<words.size(); i++) {
				Word word = words.get(i);
				Label valueLabel = new Label(0, i, word.getValue());
				writableSheet_all.addCell(valueLabel);
				Number timeLabel = new Number(1, i, word.getTime());
				writableSheet_all.addCell(timeLabel);
				Label natureLabel = new Label(2, i, word.getNature());
				writableSheet_all.addCell(natureLabel);
				if((word.getNature().contains("v") || word.getNature().contains("n")) 
						&& !"null".equalsIgnoreCase(word.getNature()) 
						&& !"en".equalsIgnoreCase(word.getNature())) {
					Label valueLabel_filter_sheet = new Label(0, j, word.getValue());
					writableSheet_filter.addCell(valueLabel_filter_sheet);
					Number timeLabel_filter_sheet = new Number(1, j, word.getTime());
					writableSheet_filter.addCell(timeLabel_filter_sheet);
					Label natureLabel_filter_sheet = new Label(2, j, word.getNature());
					writableSheet_filter.addCell(natureLabel_filter_sheet);
					j++;
				}
			}
			writableWorkbook.write();
			writableWorkbook.close();
			
		} catch (Exception e)  {
			e.printStackTrace();
		}
	}
	
}
